import {
	app,
	BrowserWindow,
	Menu,
	ipcMain,
	Tray,
	nativeImage,
	dialog
} from 'electron';
import path from 'node:path';
import Squirrel from "electron-squirrel-startup";
import WindowActionController from "./main/actions" //主进程和渲染进程通信ipcMain的处理方法
import { App } from './main/mainWindow';
import autoUpdate from './main/autoUpdate'; //自动更新服务
import creatTray from './main/tray'; //系统托盘服务
WindowActionController.init()
let mainWindow
let tray
global.isClose=false;
const autoUpdaterService = new autoUpdate(); //自动更新实例化
autoUpdaterService.init();//自动更新初始化启动更新服务

// 主进程只运行一个
const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
	app.quit();
}

app.commandLine.appendSwitch("--ignore-certificate-errors", "true");// 忽略证书相关错误
app.commandLine.appendSwitch("--disable-http-cache"); //禁用缓存
//app.commandLine.appendSwitch("disable-site-isolation-trials");

//应用是否打包
if (app.isPackaged) {
  //设置开机自动启动
  app.setLoginItemSettings({
    openAtLogin: true,
	args: ["--openAsHidden"],
  });
}

app.whenReady().then(() => {
	App.initMainWindow();
	App.loadMainWindow();
	// App.showDevTools();
	mainWindow= App.getMainWindow();
	app.on('activate', () => {
		if (BrowserWindow.getAllWindows().length === 0) {
			App.initMainWindow();
		}
	});
	app.on('second-instance', () => {
		if (mainWindow) {
			mainWindow.show();
			mainWindow.focus()
		}
	});

}).then(()=>{
	tray=creatTray(App.getMainWindow())
});

app.on('window-all-closed', () => {
	if (process.platform !== 'darwin') {
		app.quit();
	}
});

app.on('will-quit', () => {
	tray.destroy();
});